package com.lin.test;

import com.lin.mybatis.dao.EntityOneDao;
import com.lin.mybatis.dao.UserDao;
import com.lin.mybatis.entity.EntityOne;
import com.lin.mybatis.entity.Status;
import com.lin.mybatis.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @author : linwen
 * @date : 下午2:21 2021/10/6
 */
// xml中的namespace要和接口的全类名路径一致
// 接口方法名和sql标签id一致
public class 接口式编程 {
    static SqlSession session = null;
    @BeforeClass
    public static void befClass() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        session = sqlSessionFactory.openSession();
    }
    @Test
    public void test(){
        // 接口式编程的好处
        // 1：不用写长的xml配置文件路径，只用写接口的类
        // 2：接口明确了传入参数和返回参数
        UserDao mapper = session.getMapper(UserDao.class);
        User user = mapper.getUserById(3);
        System.out.println(user);

        User userById = mapper.getUserById(4);
        System.out.println(userById);
        User user1 = mapper.getUserById(3);
        System.out.println(user1);
    }
    @Test
    public void testAdd(){
        // 接口式编程的好处
        // 1：不用写长的xml配置文件路径，只用写接口的类
        // 2：接口明确了传入参数和返回参数
        UserDao mapper = session.getMapper(UserDao.class);
        User user1 = new User(0, "Tom", new Date(20210921), '女', "上海");
        int i = mapper.insertUser(user1);
        System.out.println(i);
        System.out.println(user1);
        session.commit();
    }
    @Test
    public void testUpdate(){
        // 接口式编程的好处
        // 1：不用写长的xml配置文件路径，只用写接口的类
        // 2：接口明确了传入参数和返回参数
        UserDao mapper = session.getMapper(UserDao.class);

        User user1 = new User(2, "Tom", new Date(20210921), '女', "上海");
        long l = mapper.updateUserById(user1);
        System.out.println(l);
        session.commit();
    }
    @Test
    public void testDelete(){
        // 接口式编程的好处
        // 1：不用写长的xml配置文件路径，只用写接口的类
        // 2：接口明确了传入参数和返回参数
        UserDao mapper = session.getMapper(UserDao.class);
        boolean b = mapper.deleteUserById(2);
        System.out.println(b);
        session.commit();
    }
    @Test
    public void testSelect1(){
        UserDao mapper = session.getMapper(UserDao.class);
        Map<String, Object> userReturnMap = mapper.getUserByIdReturnMap(3);
        System.out.println(userReturnMap);
    }
    @Test
    public void testSelect2(){
        UserDao mapper = session.getMapper(UserDao.class);
        Map<String, User> userReturnKeyMap = mapper.getUserReturnKeyMap();
        System.out.println(userReturnKeyMap);
    }

    @Test
    public void testQueryEntity(){
        EntityOneDao mapper = session.getMapper(EntityOneDao.class);
        EntityOne qwert = mapper.queryEntityById("qwert");
        System.out.println(qwert);
    }

    @Test
    public void testAddEntity(){
        EntityOneDao mapper = session.getMapper(EntityOneDao.class);
        mapper.addEntity(new EntityOne(UUID.randomUUID().toString(), Status.BETTER));
        System.out.println("插入成功");
        session.commit();
    }


    @AfterClass
    public static void aftClass(){
        // session是非线程安全的，每次使用都必须重新获取和释放
        session.close();
    }

}
